const URLProcessor = require('../src/utils/urlProcessor');
const VideoDownloader = require('../src/services/videoDownloader');
const TextExtractor = require('../src/services/textExtractor');
const fs = require('fs');

async function extractTextFromVideoUrl(shareUrl) {
    const urlProcessor = new URLProcessor();
    const videoDownloader = new VideoDownloader();
    const textExtractor = new TextExtractor();

    try {
        const videoId = await URLProcessor.extractDouyinVideoId(shareUrl);
        if (!videoId) {
            throw new Error('无法解析视频链接');
        }

        const videoPath = await videoDownloader.downloadVideo(videoId);
        if (!videoPath) {
            throw new Error('视频下载失败');
        }

        const textContent = await textExtractor.extractText(videoPath);

        fs.unlinkSync(videoPath);

        return textContent || '未能识别到文字内容';
    } catch (error) {
        console.error('处理失败:', error.message);
        throw error;
    }
}

module.exports = {
    extractTextFromVideoUrl
};